﻿Imports System.Collections.Generic
Imports NUnit.Framework
Imports PhDir.Model
Imports PhDir.Repository
Imports PhDir.Repository.Phone

Namespace UnitTesting.Repository
    <TestFixture()>
    Public Class TestPhoneRepository

        <Test()>
        Public Sub TotalSelectAllPhonesEqualTo26()
            Dim phoneRepository = New PhoneRepository()
            Dim phones = phoneRepository.SelectAll()
            Dim actualTotalPhones = phones.Count
            Dim expectedTotalPhones = 26

            Assert.AreEqual(expectedTotalPhones, actualTotalPhones)
        End Sub

        <Test()>
        Public Sub GetPlainPhoneShouldFail()
            Dim phoneRepository = New SearchPlainPhone()
            Dim fakePlainPhone = "111"
            Dim phones = phoneRepository.Search(fakePlainPhone)
            Dim actualTotalPhones = phones.Count
            Dim expectedTotalPhones = 0

            Assert.AreEqual(expectedTotalPhones, actualTotalPhones)
        End Sub

        <Test()>
        Public Sub GetPlainPhoneShouldSuccess()
            Dim phoneRepository = New SearchPlainPhone()
            Dim plainPhone = "03 9111 1111"
            Dim phones = phoneRepository.Search(plainPhone)
            Dim actualTotalPhones = phones.Count
            Dim expectedTotalPhones = 1

            Assert.AreEqual(expectedTotalPhones, actualTotalPhones)
        End Sub

        <Test()>
        Public Sub GetEncryptedPhoneShouldFail()
            Dim phoneRepository = New SearchEncryptedPhone()
            Dim fakePlainPhone = "111"
            Dim phones = phoneRepository.Search(fakePlainPhone)
            Dim actualTotalPhones = phones.Count
            Dim expectedTotalPhones = 0

            Assert.AreEqual(expectedTotalPhones, actualTotalPhones)
        End Sub

        <Test()>
        Public Sub GetEncryptedPhoneShouldSuccess()
            Dim phoneRepository = New SearchEncryptedPhone()
            Dim plainPhone = "03 9111 1111"
            Dim phones = phoneRepository.Search(plainPhone)
            Dim actualTotalPhones = phones.Count
            Dim expectedTotalPhones = 1

            Assert.AreEqual(expectedTotalPhones, actualTotalPhones)
        End Sub

        <Test()>
        Public Sub SearchZukoNameShouldSuccess()
            Dim phoneRepository = New PhoneRepository()
            Dim nameKeyword = "uk"
            Dim phones = phoneRepository.SearchByName(nameKeyword)
            Dim expectedName = "Zuko"
            Dim isSuccess = (From phone In phones _
                            Where phone.User.UserName = expectedName _
                            Select phone).Any

            Assert.IsTrue(isSuccess)
        End Sub

        <Test()>
        Public Sub SearchEarthDepartmentShouldSuccess()
            Dim phoneRepository = New PhoneRepository()
            Dim departmentKeyword = "ea"
            Dim phones = phoneRepository.SearchByDepartment(departmentKeyword)
            Dim expectedDepartment = "Earth"
            Dim isSuccess = (From phone In phones _
                            Where phone.User.Department.DepartmentName = expectedDepartment
                            Select phone).Any

            Assert.IsTrue(isSuccess)
        End Sub

        <Test()>
        Public Sub InsertDummyPhoneWithFakePhoneNumberShouldFail()
            Dim phoneRepository = New InsertNewPhone()
            Dim existingUser = New User With {.UserId = 1}
            Dim dummyPhone = New Phone With {.User = existingUser, .PhoneNumber = "111"}
            Dim isSuccess = phoneRepository.Insert(dummyPhone)

            Assert.IsFalse(isSuccess)
        End Sub

        <Test()>
        Public Sub InsertDummyPhoneWithNonExistingUserShouldFail()
            Dim phoneRepository = New InsertNewPhone()
            Dim nonExistingUser = New User With {.UserId = 0}
            Dim dummyPhone = New Phone With {.User = nonExistingUser, .PhoneNumber = "03 8123 1234"}
            Dim isSuccess = phoneRepository.Insert(dummyPhone)

            Assert.IsFalse(isSuccess)
        End Sub

        <Test()>
        Public Sub InsertDummyPhoneShouldSuccess()
            Dim phoneRepository = New InsertNewPhone()
            Dim nonExistingUser = New User With {.UserId = 1}
            Dim dummyPhone = New Phone With {.User = nonExistingUser, .PhoneNumber = "03 9123 1234"}
            Dim isSuccess = phoneRepository.Insert(dummyPhone)

            Assert.IsTrue(isSuccess)
        End Sub

        <Test()>
        Public Sub UpdateDummyPhoneWithFakePhoneNumberShouldFail()
            Dim phoneRepository = New UpdatePhoneNumber()
            Dim oldDummyPhone = New Phone With {.PhoneNumber = "03 9123 1234"}
            Dim newDummyPhone = New Phone With {.PhoneNumber = "111"}
            Dim isSuccess = phoneRepository.Update(oldDummyPhone, newDummyPhone)

            Assert.IsFalse(isSuccess)
        End Sub

        <Test()>
        Public Sub UpdateNonExistingPhoneNumberShouldFail()
            Dim phoneRepository = New UpdatePhoneNumber()
            Dim oldDummyPhone = New Phone With {.PhoneNumber = "07 9123 1234"}
            Dim newDummyPhone = New Phone With {.PhoneNumber = "04 9123 1234"}
            Dim isSuccess = phoneRepository.Update(oldDummyPhone, newDummyPhone)

            Assert.IsFalse(isSuccess)
        End Sub

        <Test()>
        Public Sub UpdateDummyPhoneShouldSuccess()
            Dim phoneRepository = New UpdatePhoneNumber()
            Dim oldDummyPhone = New Phone With {.PhoneNumber = "03 9123 1234"}
            Dim newDummyPhone = New Phone With {.PhoneNumber = "04 9123 1234"}
            Dim isSuccess = phoneRepository.Update(oldDummyPhone, newDummyPhone)

            Assert.IsTrue(isSuccess)
        End Sub

        <Test()>
        Public Sub DeleteNonExistingPhoneNumberShouldFail()
            Dim phoneRepository = New DeletePhoneNumber()
            Dim dummyPhone = New Phone With {.PhoneNumber = "07 9123 1234"}
            Dim isSuccess = phoneRepository.Delete(dummyPhone)

            Assert.IsFalse(isSuccess)
        End Sub

        <Test()>
        Public Sub DeleteDummyPhoneNumberShouldSuccess()
            Dim phoneRepository = New DeletePhoneNumber()
            Dim dummyPhone = New Phone With {.PhoneNumber = "04 9123 1234"}
            Dim isSuccess = phoneRepository.Delete(dummyPhone)

            Assert.IsTrue(isSuccess)
        End Sub
    End Class
End Namespace
